
clear
set more off
capture log close
*macro drop _all
cd "P:\2018\186"

gl save_dir "P:\2018\186/Martin/Decompositions_SE_extLLSMM/Graphs"


global yrmin=1985
global yrmax=2019
  *income years for children (not survey years)
 loc yrmin= ${yrmin}	
 loc yrmax= ${yrmax} 
 loc agemin=25 
 loc agemax=48 
  
use *idnr *earn* *yob *obsy *max* *edu* *emp* year woman if year>=1985 using "use\swepanel_zeros", replace


* Rename variable names

rename pmearn pm_LAB
rename pfearn pf_LAB
rename pearn pLAB

rename memp m_emplavg
rename femp f_emplavg
rename emp employ

rename medu m_schmax
rename fedu f_schmax
rename edu schmax

ren idnr newid
ren midnr m_newid
ren fidnr f_newid

gen AGEC=year-yob		// Age variables and normalization
gen AGEC1=AGEC-40		
g f_LABAGE_1=fobsy-fyob
g m_LABAGE_1=mobsy-myob

forval i=2/4{
	g AGEC`i'=AGEC1^`i'
	g f_LABAGE_`i'=f_LABAGE_1^`i'
	g m_LABAGE_`i'=m_LABAGE_1^`i'
}

* Use sample with mothers AND fathers
 keep if pm_LAB!=. & m_LABAGE_1!=. & pf_LAB!=. & f_LABAGE_1!=.
 keep if m_emplavg!=. & f_emplavg!=.
 keep if m_schmax!=. & f_schmax!=.
* Residualize all measures using mother's AND father's age (quartic)
 foreach var in pLAB employ schmax pm_LAB m_emplavg m_schmax pf_LAB f_emplavg f_schmax  {
	qui regress `var' m_LABAGE_? f_LABAGE_?   AGEC? i.year
	predict `var'_r, resid
 }
* Subsample 
 keep if inrange(year,`yrmin',`yrmax') & inrange(AGEC,`agemin',`agemax')
 tab year, su(AGEC)

rename AGEC AGE_C // so AGEC* only picks up quartic AGEC1-AGEC4

gen yr_gr=_n
replace yr_gr = yr_gr+${yrmin}-1
replace yr_gr=. if yr_gr>${yrmax}



********************************************************
*Figure A1: mother-child associations, conditional on fathers
********************************************************

*Figure A1a: (a=Sweden,b=US) Mother's IRP Conditional on father's income
gen b_irp=.
gen se_irp=.
reg pLAB i.year#c.pm_LAB c.AGEC*#c.pm_LAB i.year m_LABAGE* AGEC* , cluster(newid) 
 replace b_irp=_b[1985b.year#c.pm_LAB] if yr_gr==1985
 replace se_irp=_se[1985b.year#c.pm_LAB] if yr_gr==1985
forval y=$yrmin / $yrmax {
 replace b_irp=_b[`y'.year#c.pm_LAB] if yr_gr==`y'
 replace se_irp=_se[`y'.year#c.pm_LAB] if yr_gr==`y'
}

gen b_irp_cond=.
gen se_irp_cond=.
reg pLAB i.year#c.pm_LAB c.AGEC*#c.pm_LAB i.year m_LABAGE* AGEC*  i.year#c.pf_LAB c.AGEC*#c.pf_LAB , cluster(newid) 
 replace b_irp_cond=_b[1985b.year#c.pm_LAB] if yr_gr==1985
 replace se_irp_cond=_se[1985b.year#c.pm_LAB] if yr_gr==1985
forval y=$yrmin / $yrmax {
 replace b_irp_cond=_b[`y'.year#c.pm_LAB] if yr_gr==`y'
 replace se_irp_cond=_se[`y'.year#c.pm_LAB] if yr_gr==`y'
}
label variable yr_gr "year"
	gen ci_low 	= b_irp - 1.96*se_irp
    gen ci_high = b_irp + 1.96*se_irp
	gen ci_low_cond 	= b_irp_cond - 1.96*se_irp_cond
    gen ci_high_cond = b_irp_cond + 1.96*se_irp_cond
*Figure: 
twoway (connect b_irp yr_gr, ms(o) lc(ebblue) mc(ebblue)) (rspike ci_low ci_high  yr_gr, lc(ebblue)) /*
*/(connect b_irp_cond yr_gr, ms(t) lc(cranberry) mc(cranberry) lp(solid)) (rspike ci_low_cond ci_high_cond yr_gr, lc(cranberry)), /*
*/ ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) /*
*/legend(order(1 3) label(1 "Uncond.") label(3 "Cond. on father's income rank")) /*
*/ xsc(r(1985 2020)) xlab(1985(5)2020) 
graph export "${save_dir}/figA1a.pdf", replace
graph export "${save_dir}/figA1a.eps", replace


*Figure A1c (c=Sweden,d=US) Income-schooling regressions
cap drop b_*
cap drop se_*
cap drop ci_*
gen b_irp=.
gen se_irp=.
reg pLAB i.year#c.m_schmax c.AGEC*#c.m_schmax i.year m_LABAGE* AGEC* , cluster(newid) 
 replace b_irp=_b[1985b.year#c.m_schmax] if yr_gr==1985
 replace se_irp=_se[1985b.year#c.m_schmax] if yr_gr==1985
forval y=$yrmin / $yrmax {
 replace b_irp=_b[`y'.year#c.m_schmax] if yr_gr==`y'
 replace se_irp=_se[`y'.year#c.m_schmax] if yr_gr==`y'
}
gen b_irp_cond=.
gen se_irp_cond=.
reg pLAB i.year#c.m_schmax c.AGEC*#c.m_schmax i.year m_LABAGE* AGEC*  i.year#c.f_schmax c.AGEC*#c.f_schmax , cluster(newid) 
 replace b_irp_cond=_b[1985b.year#c.m_schmax] if yr_gr==1985
 replace se_irp_cond=_se[1985b.year#c.m_schmax] if yr_gr==1985
forval y=$yrmin / $yrmax {
 replace b_irp_cond=_b[`y'.year#c.m_schmax] if yr_gr==`y'
 replace se_irp_cond=_se[`y'.year#c.m_schmax] if yr_gr==`y'
}
label variable yr_gr "year"
	gen ci_low 	= b_irp - 1.96*se_irp
    gen ci_high = b_irp + 1.96*se_irp
	gen ci_low_cond 	= b_irp_cond - 1.96*se_irp_cond
    gen ci_high_cond = b_irp_cond + 1.96*se_irp_cond
*Figure: 
twoway (connect b_irp yr_gr, ms(o) lc(ebblue) mc(ebblue)) (rspike ci_low ci_high  yr_gr, lc(ebblue)) /*
*/(connect b_irp_cond yr_gr, ms(t) lc(cranberry) mc(cranberry) lp(solid)) (rspike ci_low_cond ci_high_cond yr_gr, lc(cranberry)), /*
*/ ytitle("Persistence") xtitle("Year") graphr(fc(white) c(white)) /*
*/legend(order(1 3) label(1 "Uncond.") label(3 "Cond. on father's schooling")) /*
*/ xsc(r(1985 2020)) xlab(1985(5)2020) 

graph export "${save_dir}/figA1c.pdf", replace
graph export "${save_dir}/figA1c.eps", replace








**************************************************************
*Figure 5: Parental assortative mating in Sweden and the US
**************************************************************

 drop if year>1995 & year<2007
 
 gen t=(year>=2007)
 replace pLAB_r = pLAB_r+50
  replace pm_LAB_r = pm_LAB_r+50
  replace pf_LAB_r = pf_LAB_r+50
  sum m_schmax
  replace m_schmax_r = m_schmax_r+r(mean)
  sum f_schmax
  replace f_schmax_r = f_schmax_r+r(mean)
*Figure 5a
 binscatter  m_schmax_r f_schmax_r, by(t)	ytitle("Mean schooling (mother)") xtitle("Schooling (father)") ///
  legend(order(1 2) label(1 "1985-1995") label(2 "2008-2019")) msymbol(O Oh)

  graph export "${save_dir}/fig5a.pdf", replace
  graph export "${save_dir}/fig5a.eps", replace
*Figure 5c
 binscatter pm_LAB_r pf_LAB_r, by(t)	ytitle("Mean income rank (mother)") xtitle("Father's income rank") ///
  legend(order(1 2) label(1 "1985-1995") label(2 "2008-2019")) msymbol(O Oh)
  graph export "${save_dir}/fig5c.pdf", replace
  graph export "${save_dir}/fig5c.eps", replace
